<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\AccountCate;
use think\App;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use app\common\model\Assets;
use think\response\Json;

class Account extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Account';


    //关联模型
    protected $hasOne = ['user', 'take'];

    protected $sort = 'id,desc';

    //账户流水
    public function index()
    {
        return $this->setTable('index');
    }

    //奖金明细
    public function bonus()
    {
        $extraMap['is_bonus'] = 1;
        $extraMap['is_village'] = 0;
        if($pool_id = $this->request->param('pool_id', '')){
            $extraMap['pool_id'] = $pool_id;
        }
        if($contract_pool_id = $this->request->param('contract_pool_id', '')){
            $extraMap['contract_pool_id'] = $contract_pool_id;
        }
        return $this->setTable('bonus', $extraMap);
    }

    //充值明细
    public function recharge()
    {
        return $this->setTable('recharge', ['is_admin' => 1]);
    }

    public function setTable($action, $extra_map = [])
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'nickname', 'title' => '昵称', 'templet' => '<div>{{d.user.nickname}}</div>'])
            ->addRow(['field' => 'code', 'title' => '编号', 'templet' => '<div>{{d.user.code}}</div>'])
            ->addRow(['field' => 'mobile', 'title' => '手机号', 'templet' => '<div>{{d.user.mobile}}</div>'])
            ->addRow(['field' => 'type', 'title' => '账户'])
            ->addRow(['field' => 'intro', 'title' => '说明', 'width' => 150]);
        if ($action != 'index') {
            $tableFront->addRow(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>']);
        } else {
            $tableFront->addRow(['field' => 'front', 'title' => '前余额'])
                ->addRow(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'])
                ->addRow(['field' => 'after', 'title' => '后余额'])
                ->addRow(['title' => '状态', 'templet' => '<div>{{d.is_grant == 2?"正常":"-"}}</div>', 'fixed' => 'right', 'align' => 'center']);
        }
        if($action != 'recharge'){
            $tableFront->addRow(['title' => '来源会员编号', 'templet' => '<div>{{d.take ? d.take.code:"-"}}</div>'])
                ->addRow(['title' => '来源会员姓名', 'templet' => '<div>{{d.take ? d.take.nickname:"-"}}</div>'])
                ->addRow(['title' => '来源会员手机号', 'templet' => '<div>{{d.take ? d.take.mobile:"-"}}</div>']);
        }
        $tableFront->addRow(['field' => 'create_time', 'title' => '时间', 'width' => 150])
            ->addRow(['field' => 'msg', 'title' => '备注']);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->setSearch($this->setSearch($action))
            ->addExtraMap($extra_map)
            ->addTopButtons(['export'])
            ->fetch();
    }


    public function setSearch($action)
    {
        $search = TableFront::getInstance()
            ->setSearch(['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'])
            ->setSearch(['type', '账户类型', 'select', Assets::getFields(3)])
            ->setSearch(['amount_min', '最低金额', 'input', '', '', '', ''])
            ->setSearch(['amount_max', '最高金额', 'input', '', '', '', ''])
            ->setSearch(['intro', '说明', 'input', '', '', '', '']);
        switch ($action) {
            case "index":
                $search->setSearch(['source', '来源会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']);
                break;
            case "bonus":
                $search->setSearch(['source', '来源会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'])
                    ->setSearch(['category', '奖金类型', 'select', AccountCate::cateList(2)]);
                break;
        }
        return $search;
    }

    public function getList($takeOver = true): Json
    {
        return parent::getList($takeOver); // TODO: Change the autogenerated stub
    }

    public function export()
    {
        $s = $this->setMap($this->request->get('name'));
        $s['with'] = $this->hasOne;

        $name = '后台充值明细' . date('Y-m-d H:i:s');
        if (!isset($s['param']['is_admin'])) {
            if (!isset($s['param']['is_bonus'])) {
                $name = '账户明细' . date('Y-m-d H:i:s');
            } else {
                $name = '奖金明细' . date('Y-m-d H:i:s');
            }
        }

        $title = TableFront::getInstance()->getFieldTable($this->request->get('name'));
        if (empty($title)) {
            return json(['code' => -1, 'msg' => '导出失败，请清除缓存后重试！']);
        }

        $s['filed'] = 'id,uid,type,intro,amount,create_time,source,fx,is_user,front,after';
        $list = $this->model::getSelect($s);

        foreach ($list['data'] as &$v) {
            $v['code'] = $v->user->code;
            $v['nickname'] = $v->user->nickname;
            $v['mobile'] = $v->user->mobile;
            $v['amount'] = $v->fx . $v->amount;
            if (!isset($s['param']['is_admin'])) {
                $v['take_code'] = $v->take ? $v->take->code : '-';
                $v['take_nickname'] = $v->take ? $v->take->nickname : '-';
                $v['take_mobile'] = $v->take ? $v->take->mobile : '-';
            }
            unset($v->is_user);
            unset($v->uid);
            unset($v->fx);
            unset($v->user);
            unset($v->take);
            unset($v->source);
        }

        return json(['code' => 1, 'result' => ['data' => $list['data']->toArray(), 'title' => $title, 'name' => $name]]);
    }

}
